﻿using BXH.Movie.Bll;
using BXH.Movie.Forms;
using BXH.Movie.FrmAdmin;
using BXH.Movie.Model;
using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BXH.Movie.FrmUsers
{
    public partial class FrmMain : CCWin.CCSkinMain
    {


        public static FrmMain _FrmMain;

        public static string BasePath = AppDomain.CurrentDomain.BaseDirectory + "Images\\";

        public List<Image> ImageList = new List<Image>();

        private MovieInfoService movieService = new MovieInfoService();
        public FrmMain()
        {
            InitializeComponent();
            _FrmMain = this;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.LoadingListImage();
            //this.PnlContentMain.VerticalScroll.Visible = true;
       
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.btnMovieList_Click(null, null);
        }

        private void FrmMain_Shown(object sender, EventArgs e)
        {
            Console.WriteLine(22);
        }

        private void LoadingListImage()
        {
            //var files = Directory.GetFiles(BasePath, "*.*", SearchOption.AllDirectories).Where(s => s.ToLower().EndsWith(".jpg") || s.ToLower().EndsWith(".png")).ToArray();
            //foreach (var file in files)
            //{
            //    imageList.Add(Image.FromFile(file));
            //}

            var movies = movieService.GetMovieInfos().OrderBy(x=>x.Name).Skip(0).Take(5);
            var imageUrls = movies.Select(m => m.Image);
            var imageList = imageUrls.Select(url => Image.FromFile(Path.Combine(FrmMain.BasePath, url))).ToList();
            ImageList.AddRange(imageList);
        }



        /// <summary>
        /// 电影管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMovieList_Click(object sender, EventArgs e)
        {
            var uCMovieMain = UControlCacheHelper.GetUControl<UCMovieMain>();
            uCMovieMain.Movies = movieService.GetMovieInfos().ToList();
            this.SetManageShow(uCMovieMain);
        }
        /// <summary>
        /// 订单管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOrderManager_Click(object sender, EventArgs e)
        {
            if (Program.LoginUser == null)
            {
                GoToLogin();
            }
            if (Program.LoginUser == null)
            {
                MessageBoxEx.Show("请先登录");
                return;
            }
            UCOrderManager uCOrderManager = UControlCacheHelper.GetUControl<UCOrderManager>();
            this.SetManageShow(uCOrderManager);


        }
        /// <summary>
        /// 个人信息管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInformationManager_Click(object sender, EventArgs e)
        {
            if (Program.LoginUser == null)
            {
                GoToLogin();
            }
            if (Program.LoginUser == null)
            {
                MessageBoxEx.Show("请先登录");
                return;
            }
            UCInformationManager uCInformationManager = UControlCacheHelper.GetUControl<UCInformationManager>();
            this.SetManageShow(uCInformationManager);

        }

        public void GoToLogin()
        {
            FrmLogin frmLogin = new FrmLogin();
            frmLogin.LoginSuccess += FrmLogin_LoginSuccess;
            frmLogin.ShowDialog();
        }
        /// <summary>
        /// 登录成功事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmLogin_LoginSuccess(object sender, EventArgs e)
        {

            switch (Program.LoginUser.RoleId)//判断他的角色
            {
                case RoleType.Administrator:
                    this.btnToAdmin.Visible = true;
                    //this.btnToAdministrator.Visible = true;
                    break;
                case RoleType.Admin:
                    this.btnToAdmin.Visible = true;
                    break;
            }
            lbNick.Text = $"账号：{Program.LoginUser.UserCode}";
            plLogin.Visible = false;
            plUserInfo.Visible = true;

        }

        /// <summary>
        /// 影评管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //private void btnCinecismManagement_Click(object sender, EventArgs e)
        //{
        //   var uCCinecismManagement= UControlCacheHelper.GetUControl<UCCinecismManagement>();
        //    this.SetManageShow(uCCinecismManagement);
        //}
        //定时显示图片
        private void timer1_Tick(object sender, EventArgs e)
        {

        }




        /// <summary>
        /// Fill 停靠
        /// </summary>
        /// <param name="control"></param>
        public void SetManageShow(Control control)
        {
            this.PnlContentMain.Controls.Clear();
            this.PnlContentMain.Controls.Add(control);
            this.PnlContentMain.Controls[0].Dock = DockStyle.Fill;
        }
        /// <summary>
        /// 居中停靠
        /// </summary>
        /// <param name="control"></param>
        public void SetManageOtherShow(Control control)
        {
            this.PnlContentMain.Controls.Clear();
            this.PnlContentMain.Controls.Add(control);
            this.PnlContentMain.Controls[0].Location = new Point((this.PnlContentMain.Width - control.Width) / 2, (this.PnlContentMain.Height - control.Height) / 2);
        }
        //退出
        private void btnLoginOut_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否退出登录？？？", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Program.LoginUser = null;

                this.plLogin.Visible = true;
                this.plUserInfo.Visible =
                this.btnToAdmin.Visible = false;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            GoToLogin();
        }

        private void btnToAdmin_Click(object sender, EventArgs e)
        {
            Form adminForm = Application.OpenForms["FrmMainAdmin"];  //查找是否打开过about窗体  
            if ((adminForm == null) || (adminForm.IsDisposed)) //如果没有打开过
            {
                adminForm = new FrmMainAdmin();
                adminForm.Show();   //打开子窗体出来
            }
            else
            {
                adminForm.Activate(); //如果已经打开过就让其获得焦点  
                adminForm.WindowState = FormWindowState.Normal;//使Form恢复正常窗体大小
            }
        }

        private void btnToAdministrator_Click(object sender, EventArgs e)
        {
            Form superAdminForm = Application.OpenForms["FrmMainSuperAdmin"];  //查找是否打开过about窗体  
            if ((superAdminForm == null) || (superAdminForm.IsDisposed)) //如果没有打开过
            {
                //superAdminForm = new FrmMainSuperAdmin();
                superAdminForm.Show();   //打开子窗体出来
            }
            else
            {
                superAdminForm.Activate(); //如果已经打开过就让其获得焦点  
                superAdminForm.WindowState = FormWindowState.Normal;//使Form恢复正常窗体大小
            }
        }

        private void PnlContentMain_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
